import Vue from 'vue'
// 导入vuex全局状态管理插件
import Vuex from 'Vuex'
import baseCenter from '@/store/modules/baseCenter.js'
// 购物车下单
import CartOrder from '@/store/modules/cartOrder/CartOrder.js'
// 订单详情
import order from '@/store/modules/Order/order.js'
// 地址
import address from '@/store/modules/address/address.js'
Vue.use(Vuex)
const state = {
  // 首页导航栏
  indexTabBar: [],
  // 首页数据
  indexData: {},
  sortData: {},
  goodsList: [],
  // 购物车列表
  goodsCarts: [],
  // 我的信息
  myUserInfo: {
    // 是否登陆
    active: false,
    phone: '',
    name: '请先登陆',
    password: '',
    sasuke: '请先登陆',
    date: '',
    src: 'https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fimg3.doubanio.com%2Fview%2Fgroup_topic%2Fl%2Fpublic%2Fp290307260.jpg&refer=http%3A%2F%2Fimg3.doubanio.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1654931569&t=17d80a6df404cbf39cfcb69c5756b1c0',
    // 收藏
    collect: [],
    // 优惠券
    coupon: [],
    // 我的足迹
    footprint: []
  },
  // 地址列表
  addressList: []
}
const mutations = {
  // 请求首页导航栏数据
  getIndexBar (state, res) {
    state.indexTabBar = res
  },
  // 存储首页信息
  getIndexData (state, data) {
    state.indexData = data
  },
  // 分类商品
  getSortData (state, data) {
    state.sortData = data
  },
  // 商品列表
  getGoodsList (state, data) {
    state.goodsList = data
  },
  // // 购物车列表
  getGoodsCarList (state) {
    state.goodsCarts = JSON.parse(window.localStorage.getItem('carList')) || []
  },
  // 添加用户信息
  setUserInfo (state, data) {
    state.myUserInfo = data
  },

  // 编辑地址 获取最新地址
  getAddressList (state) {
    const list = JSON.parse(window.localStorage.getItem('addressList'))
    state.addressList = list
  }
}
const actions = {
  // app导航栏
  asyncBarList (context, res) {
    context.commit('getIndexBar', res)
  },
  // 首页app数据缓存
  asyncGetIndexData (context, data) {
    context.commit('getIndexData', data)
  },
  // 分类
  asyncGetSortData (context, data) {
    context.commit('getSortData', data)
  },
  // 异步商品列表
  asyncGetGoodsList (context, data) {
    context.commit('getGoodsList', data)
  }
}
const getters = {
  // 首页商品数据
  goodsList (state) {
    return state.indexData
  },
  index (state) {
    return state.goodsList
  }
}
const modules = {
  namespaced: true,
  baseCenter,
  CartOrder,
  address,
  order
}
// 导出全局 Store
export default new Vuex.Store({
  actions,
  mutations,
  state,
  getters,
  modules,
})